# -*- coding:utf-8 -*-
import redis
from app_config import RedisConfig

REDIS_HOST = RedisConfig.REDIS_HOST
REDIS_PORT = RedisConfig.REDIS_PORT
REDIS_DB = RedisConfig.REDIS_DB
REDIS_PASSWORD = RedisConfig.REDIS_PASSWORD

# 数据库的连接池
if REDIS_PASSWORD:
	db_conn_pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, password=REDIS_PASSWORD)
else:
	db_conn_pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)


def set_cache_key_value(key, value, end_time=None):
	"""
	设置通用型存储缓存方法
	:param key:
	:param value:
	:param end_time:
	:return:
	"""
	r = redis.Redis(connection_pool=db_conn_pool)
	r.set(key, value)
	if end_time is not None:
		print("=====set_cache_key_value=======")
		r.expire(key, end_time)


def get_cache_key_value(key):
	"""
	根据key获取value
	:param key:
	:return:
	"""
	print("=====key===", key)
	r = redis.Redis(connection_pool=db_conn_pool)
	if r.get(key):
		return r.get(key).decode()
	else:
		return r.get(key)
